import 'package:fish_redux/fish_redux.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'action.dart';
import 'state.dart';

Reducer<AlipayViewState> buildReducer() {
  return asReducer(
    <Object, Reducer<AlipayViewState>>{
      AlipayViewAction.action: _onAction,
      AlipayViewAction.onWebViewCreate: _onWebViewCreate,
      AlipayViewAction.onPageFinished: _onPageFinished,
    },
  );
}

AlipayViewState _onAction(AlipayViewState state, Action action) {
  final AlipayViewState newState = state.clone();
  return newState;
}

AlipayViewState _onPageFinished(AlipayViewState state, Action action) {
  final AlipayViewState newState = state.clone()..isLoaded = true;
  return newState;
}

AlipayViewState _onWebViewCreate(AlipayViewState state, Action action) {
  final AlipayViewState newState = state.clone()
    ..webViewController = action.payload;
  if (state.url != null) {
    newState.webViewController.loadUrl(state.url);
  }
  return newState;
}
